package com.gxuwz.business.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gxuwz.business.entity.SysUser;
import com.gxuwz.util.Result;


/**
 * A data access object (DAO) providing persistence and search support for
 * SysUser entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.gxuwz.TravelToolWeb.business.dao.SysUser
 * @author MyEclipse Persistence Tools
 */

@Repository("sysUserDAO")
public class SysUserDAO extends BaseDaoImpl<SysUserDAO>{
	
	/**
	 * 查询全部
	 *
	 */
	@SuppressWarnings("unchecked")
	public List<SysUser> findAll(){
		String queryString = "from SysUser where 1=1";
		return (List<SysUser>) getHibernateTemplate().find(queryString);
	}
	
	public SysUser findByName(String userId){
		String queryString = "from SysUser where userName = '"+userId+"' or userId = '"+userId+"'";
		List list = getHibernateTemplate().find(queryString);
		if(list.size()>0){
			return (SysUser) list.get(0);
		}
		return null;
	}
	
	/**
	 * 根据property属性的值value获取对象
	 * @param property SysUser对象的属性
	 * @param value 值
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public SysUser findSysUserByProperty(String property,String value){
		SysUser sysUser = null;
		String hql = "from SysUser where "+property+" = '"+value+"'";
		List<SysUser> list = (List<SysUser>) getHibernateTemplate().find(hql);
		if(null != list && 0<list.size()){
			sysUser = list.get(0);
		}
		return sysUser;
	}
	
	
	/**
	 * 只更新密码
	 */
	public boolean updatePassword(String newPassword,SysUser s){
		boolean isSucceed = false;
		String hql = "update SysUser set userPassword=? where userId=?";
		try {
			int count = super.getHibernateTemplate().bulkUpdate(hql,newPassword, s.getUserId());
//			System.out.println("更改了"+count+"条数据");
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSucceed;
	}
	
	/**
	 * 更新用户信息
	 */
	public boolean update(SysUser user){
		boolean isSucceed = false;
		try {
			getHibernateTemplate().update(user);
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return isSucceed;
	}
	
	/**
	 * 更新信息
	 * @return
	 */
	public SysUser findById(String userId){
		String hql = "from SysUser where userId = ?";
		try {
			return (SysUser) getHibernateTemplate().find(hql, userId).get(0);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 上传头像
	 * @param sysUser
	 * @return
	 */
	public Result updateUserFace(SysUser sysUser) {
		Result result = new Result();
		String hql = "update SysUser set userImg = ? where userId = ?";
		try {
			getHibernateTemplate().bulkUpdate(hql, sysUser.getUserImg(), sysUser.getUserId());
			result.setCode(0);
			result.setMsg("上传成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
}